﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Summary description for UserBus
/// </summary>
public class UserBus
{
    UserDAL udal;
    public UserBus()
	{
        udal = new UserDAL();
    }
    public bool Insert_UpdateUser(User user)
    {
        bool check = false;
        if (udal.Insert_UpdateUser(user) > 0)
        {
            check = true;
        }
        return check;
    }
    public bool checkLogin(string username, string password) 
    {
        bool check = false;
        DataTable dt = new DataTable();
        dt = udal.Login(username, password);
        if (dt.Rows.Count > 0) 
        {
            check = true;   
        }
        return check;
    }
    public DataTable getUserByUser_Name(string user_name) 
    {
        return udal.getUserByUserName(user_name);
    }
    public DataTable getUserByEMAIL(string email) 
    {
        return udal.getUserByEmail(email);
    }
    public User getUserByUserName(string user_name)
    {
        User urs = new User();
        DataTable dt = udal.getUserByUserName(user_name);
        urs.User_id = Convert.ToInt32(dt.Rows[0]["user_id"].ToString());
        urs.User_name = dt.Rows[0]["user_name"].ToString();
        urs.Password = dt.Rows[0]["password"].ToString();
        urs.FullName = dt.Rows[0]["full_name"].ToString();
        urs.Birthday = Convert.ToDateTime(dt.Rows[0]["birthday"].ToString());
        urs.Email = dt.Rows[0]["email"].ToString();
        urs.Sex = Convert.ToBoolean(dt.Rows[0]["sex"].ToString());
        urs.Address = dt.Rows[0]["address"].ToString();
        urs.Phone = dt.Rows[0]["phone"].ToString();
        urs.User_group_id = Convert.ToInt32(dt.Rows[0]["user_group_id"].ToString());
        return urs;
    }

    public User getUserByEmail(string email) 
    {
        User urs = new User();
        DataTable dt = udal.getUserByEmail(email);
        urs.User_id = Convert.ToInt32(dt.Rows[0]["user_id"].ToString());
        urs.User_name = dt.Rows[0]["user_name"].ToString();
        urs.Password = dt.Rows[0]["password"].ToString();
        urs.FullName = dt.Rows[0]["full_name"].ToString();
        urs.Birthday = Convert.ToDateTime(dt.Rows[0]["birthday"].ToString());
        urs.Email = dt.Rows[0]["email"].ToString();
        urs.Sex = Convert.ToBoolean(dt.Rows[0]["sex"].ToString());
        urs.Address = dt.Rows[0]["address"].ToString();
        urs.Phone = dt.Rows[0]["phone"].ToString(); 
        urs.User_group_id = Convert.ToInt32(dt.Rows[0]["user_group_id"].ToString());
        return urs;
    }

    public User getUserByID(int user_id)
    {
        User urs = new User(); 
        DataTable dt = udal.getUserByID(user_id);
        if (!dt.HasErrors && dt != null)
        {
            foreach (DataRow r in dt.Rows)
            {
                urs=new User(
                    Convert.ToInt32(dt.Rows[0]["user_id"]),
                    r["user_name"].ToString(),
                    r["password"].ToString(),
                    r["full_name"].ToString(),
                    Convert.ToDateTime(r["birthday"].ToString()),
                    r["email"].ToString(),
                    Convert.ToBoolean(r["sex"].ToString()),
                    r["address"].ToString(),
                    r["phone"].ToString(),
                    Convert.ToInt32(r["user_group_id"].ToString()),
                    Convert.ToDateTime(r["date_added"])
                    );
            }
      
        }
        else
        {
            urs = new User();
        }
        return urs;
    }
    public DataTable getAllUsers()
    {
        return udal.getAllUsers();
    }
    public bool deleteUserByID(int user_id)
    {
        if (udal.DeleteUserByID(user_id) != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public string MaHoaMD5(string sinput)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(sinput);
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] hash = md5.ComputeHash(bytes);
        StringBuilder sb = new StringBuilder();
        foreach (byte b in hash)
        {
            sb.Append(string.Format("{0:X2}", b));
        }

        return sb.ToString();
    }
}